<%@ page language="java" contentType="text/html; charset=UTF-8"
	import="java.io.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>资源下载</title>
	</head>
	<body>
		<%
			try{
				String url = request.getParameter("url");
				String fileName = url.substring(url.lastIndexOf("/")+1);
				String base = pageContext.getServletContext().getRealPath("/") + "resource";
				String fileDisplayName = fileName;
				String encode = request.getCharacterEncoding();
				fileName = new String(fileName.getBytes("UTF-8"),
						encode == null ? "UTF-8" : encode);
				fileDisplayName = new String(fileDisplayName.getBytes("GBK"), "iso8859-1");

				File file = new File(base + "/" + fileName);

				//设置请求头
				String type = application.getMimeType(base + "/" + fileName);
				response.setContentType("application/x-download");
				response.setHeader("Content-Disposition", "attachment;filename=" + fileDisplayName);
				response.setHeader("Content-Length", file.length() + "");

				InputStream in = new FileInputStream(file);
				OutputStream outp = response.getOutputStream();

				BufferedInputStream bufin = new BufferedInputStream(in);
				BufferedOutputStream bufout = new BufferedOutputStream(outp);

				byte[] temp = new byte[1024];
				while (bufin.read(temp) > 0) {
					bufout.write(temp);
					bufout.flush();
				}
				bufout.close();
				bufin.close();
				outp.close();
				in.close();

				out.clear();
				out = pageContext.pushBody();
			}catch(Exception e){
				e.printStackTrace();
			}
		%>

	</body>
</html>